﻿
//  此文件中定义CPU指令字节码与指令的对应关系，以及CPU的寻址方式
namespace DA6502.CPU
{
    using addr = System.UInt16;

    /// <summary>
    /// 6502 CPU 寻址方式枚举
    /// </summary>
    public enum ADDRESSING_METHOD
    {
        /// <summary>
        /// 累加器型 OPC A
        /// </summary>
        Accumulator, // OPC A ( OPC A )

        /// <summary>
        /// 绝对型 OPC $LLHH
        /// </summary>
        Absolute, // OPC abs ( OPC $LLHH )

        /// <summary>
        /// 绝对X变址型 OPC $LLHH,X
        /// </summary>
        AbsoluteX, // OPC abs,X ( OPC $LLHH,X )

        /// <summary>
        /// 绝对Y变址型 OPC $LLHH,Y
        /// </summary>
        AbsoluteY, // OPC abs,Y ( OPC $LLHH,Y )

        /// <summary>
        /// 立即型 OPC #$BB
        /// </summary>
        Immediate, // OPC # ( OPC #$BB )

        /// <summary>
        /// 隐含型 OPC
        /// </summary>
        Implied, // OPC impl ( OPC )

        /// <summary>
        /// 间接型 OPC ($LLHH)
        /// </summary>
        Indirect, // OPC ind ( OPC ($LLHH) )

        /// <summary>
        /// 索引间接型 OPC ($LL, X)
        /// </summary>
        IndexedIndirect, // OPC X,ind ( OPC ($LL,X) )

        /// <summary>
        /// 间接索引型 OPC ($LL), Y
        /// </summary>
        IndirectIndexed, // OPC ind,Y ( OPC ($LL),Y )

        /// <summary>
        /// 相对型 OPC *+rel
        /// </summary>
        Relative, // OPC rel ( OPC $BB )

        /// <summary>
        /// 零页型 OPC $LL
        /// </summary>
        ZeroPage, // OPC zpg ( OPC $LL )

        /// <summary>
        /// 零页X变址型 OPC $LL, X
        /// </summary>
        ZeroPageX, // OPC zpg,X ( OPC $LL,X )

        /// <summary>
        /// 零页Y变址型 OPC $LL, Y
        /// </summary>
        ZeroPageY, // OPC zpg,Y ( OPC $LL,Y )
    }

    /// <summary>
    /// CPU指令集的映射，以及寻址方式的实现
    /// </summary>
    public partial class CPU
    {
        /// <summary>
        /// 当前执行的CPU指令的寻址方式
        /// </summary>
        private ADDRESSING_METHOD currAdMethod;

        /// <summary>
        /// 当前执行的CPU指令的操作数
        /// </summary>
        public string currOpNum = string.Empty;

        /// <summary>
        /// 当前执行的CPU指令的字节码
        /// </summary>
        public byte currOpCod;

        #region ==============================| 6502 CPU 寻址方式 |==============================
        /// <summary>
        /// 立即数型， CMD #$LL
        /// </summary>
        protected byte AddressImmediate()
        {
            byte ll = IF();
            currAdMethod = ADDRESSING_METHOD.Immediate;
            currOpNum = @"#$" + ll.ToString("X2");
            return ll;
        }

        /// <summary>
        /// 零页寻址： CMD $LL
        /// </summary>
        protected addr AddressingZeroPage()
        {
            addr ll = IF();
            currAdMethod = ADDRESSING_METHOD.ZeroPage;
            currOpNum = "$" + ll.ToString("X2");
            return ll;
        }

        /// <summary>
        /// 零页X变址寻址： CMD $LL, X
        /// </summary>
        protected addr AddressingZeroPageX()
        {
            byte ll = IF();
            addr ad = (addr)((ll + X) & 0x00FF);
            currAdMethod = ADDRESSING_METHOD.ZeroPageX;
            currOpNum = "$" + ll.ToString("X2") + ", X";
            return ad;
        }

        /// <summary>
        /// 零页Y变址寻址： CMD $LL, Y
        /// </summary>
        /// 需要注意的是，这一模式只能用于LDX和STX指令
        protected addr AddressingZeroPageY()
        {
            byte ll = IF();
            addr ad = (addr)((ll + Y) & 0x00FF);
            currAdMethod = ADDRESSING_METHOD.ZeroPageY;
            currOpNum = "$" + ll.ToString("X2") + ", Y";
            return ad;
        }

        /// <summary>
        /// 相对寻址： CMD *+$LL
        /// </summary>
        protected addr AddressingRelative()
        {
            byte ll = IF();
            sbyte sop = (sbyte)ll;  //  相对寻址操作数为一个字节，这个字节被视为有符号字节，也就是说相对可以向前也可以向后。
            addr ad = (addr)(PC + sop);
            ll = (byte)(sop < 0 ? (~ll) + 1 : ll);
            currAdMethod = ADDRESSING_METHOD.Relative;
            currOpNum = "*" + (sop >= 0 ? "+$" : "-$") + ll.ToString("X2") + " ($" + (PC + sop).ToString("X4") + ")";
            return ad;
        }

        /// <summary>
        /// 绝对寻址： CMD $HHLL
        /// </summary>
        protected addr AddressingAbsolute()
        {
            byte ll = IF();
            byte hh = IF();
            addr ad = AddrJoin(hh, ll);
            currAdMethod = ADDRESSING_METHOD.Absolute;
            currOpNum = "$" + ad.ToString("X4");
            return ad;
        }

        /// <summary>
        /// 绝对X变址寻址：CMD $HHLL, X
        /// </summary>
        protected addr AddressingAbsoluteX()
        {
            byte ll = IF();
            byte hh = IF();
            addr ad = (addr)(AddrJoin(hh, ll) + X);
            currAdMethod = ADDRESSING_METHOD.AbsoluteX;
            currOpNum = "$" + (ad - X).ToString("X4") + ", X";
            return ad;
        }

        /// <summary>
        /// 绝对Y变址寻址： CMD $HHLL, Y
        /// </summary>
        protected addr AddressingAbsoluteY()
        {
            byte ll = IF();
            byte hh = IF();
            addr ad = (addr)(AddrJoin(hh, ll) + Y);
            currAdMethod = ADDRESSING_METHOD.AbsoluteY;
            currOpNum = "$" + (ad - Y).ToString("X4") + ", Y";
            return ad;
        }

        /// <summary>
        /// 间接寻址： CMD ($HHLL) ，只有JMP指令使用
        /// </summary>
        protected addr AddressingIndirect()
        {
            byte ll = IF();
            byte hh = IF();

            //  取得【操作数指定的地址】ad，跳转目标地址就在这个
            //  【操作数指定地址】ad里保存着（不是这个【操作数指定地址】本身）
            addr ad = AddrJoin(hh, ll);
            addr op = ad;
            addr adl = ad;
            addr adh = (addr)(ad + 1);
            if ((ll & 0xFF) == 0xFF)
            {
                //  CPU的BUG：JMP ($xxyy)，或JMP间接，如果指定地址的下8位是$FF，则不会推进页;上面的8位是从$xx00取的
                adh = (addr)(ad & 0xFF00);
            }
            ad = AddrJoin(IO[adh], IO[adl]);
            currAdMethod = ADDRESSING_METHOD.Indirect;
            currOpNum = "($" + op.ToString("X4") + ")" + " ($" + ad.ToString("X4") + ")";
            return ad;
        }

        /// <summary>
        /// 索引间接型寻址: CMD ($LL, X)
        /// </summary>
        protected addr AddressingIndexedIndirect()
        {
            byte ll = (byte)(IF() + X);
            addr ad = AddrJoin(IO[(ushort)((ll + 1) & 0x00FF)], IO[ll]);
            currAdMethod = ADDRESSING_METHOD.IndexedIndirect;
            currOpNum = "($" + (ll - X).ToString("X2") + ", X)";
            return ad;
        }

        /// <summary>
        /// 间接索引寻址：CMD ($LL), Y
        /// </summary>
        [System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
        protected addr AddressingIndirectIndexed()
        {
            byte ll = IF();
            addr ad = AddrJoin(IO[(ushort)((ll + 1) & 0x00FF)], IO[ll]);
            ad += Y;
            currAdMethod = ADDRESSING_METHOD.IndirectIndexed;
            currOpNum = "($" + ll.ToString("X2") + "), Y";
            return ad;
        }

        #endregion

        #region ==============================| 6502 CPU 指令 0x |==============================

        //================================================================================
        //  以下为6502CPU指令模拟
        //  变量 ad : address，寻址得到的地址
        //  变量 op : operator，取指取得的操作数
        //  变量 ps : Page Same （与 BoundaryCrossed 相反），寻址跨页时为false， 同一页内为true
        //  变量 br : Branch（分支）判断条件是否为真
        //  返回值为该命令所需要的CPU CLock周期数
        //================================================================================

        /// <summary>
        /// BRK 
        /// BRK中断
        /// </summary>
        [Instruction(true, 1, 7)]
        protected int Cmd00()
        {
            Brk();
            return 7;
        }

        /// <summary>
        /// ORA (oper, X)
        /// 逻辑或（索引间接型）
        /// </summary>
        [Instruction(true, 2, 6)]
        protected int Cmd01()
        {
            addr ad = AddressingIndexedIndirect();
            Ora(ad);
            return 6;
        }

        /// <summary>
        /// KIL
        /// </summary>
        [Instruction(false, 1, 1)]
        protected int Cmd02()
        {
            currAdMethod = ADDRESSING_METHOD.Implied;
            Kil();
            return 0;
        }

        /// <summary>
        /// SLO (oper, x)
        /// 算术左移内容一位并与A比较，结果存入A（索引间接型）
        /// </summary>
        [Instruction(false, 2, 8)]
        protected int Cmd03()
        {
            addr ad = AddressingIndexedIndirect();
            Slo(ad);
            return 8;
        }

        /// <summary>
        /// NOP oper
        /// 空操作(零页)
        /// </summary>
        [Instruction(false, 2, 3)]
        protected int Cmd04()
        {
            addr ad = AddressingZeroPage();
            Nop(ad);
            return 3;
        }

        /// <summary>
        /// ORA oper
        /// 逻辑或（零页）
        /// </summary>
        [Instruction(true, 2, 3)]
        protected int Cmd05()
        {
            addr ad = AddressingZeroPage();
            Ora(ad);
            return 3;
        }

        /// <summary>
        /// ASL oper
        /// 算术左移（零页）
        /// </summary>
        [Instruction(true, 2, 5)]
        protected int Cmd06()
        {
            addr ad = AddressingZeroPage();
            Asl(ad);
            return 5;
        }

        /// <summary>
        /// SLO oper
        /// 算术左移内容一位并与A比较，结果存入A（零页）
        /// </summary>
        [Instruction(false, 2, 5)]
        protected int Cmd07()
        {
            addr ad = AddressingZeroPage();
            Slo(ad);
            return 5;
        }

        /// <summary>
        /// PHP
        /// 处理器状态入栈
        /// </summary>
        [Instruction(true, 1, 3)]
        protected int Cmd08()
        {
            Php();
            return 3;
        }

        /// <summary>
        /// ORA #oper
        /// 逻辑或（立即型）
        /// </summary>
        [Instruction(true, 2, 2)]
        protected int Cmd09()
        {
            byte op = AddressImmediate();
            Ora(op);
            return 2;
        }

        /// <summary>
        /// ASL 
        /// 算术左移（累加器型）
        /// </summary>
        [Instruction(true, 2, 2)]
        protected int Cmd0A()
        {
            Asl();
            return 2;
        }

        /// <summary>
        /// ANC #oper
        /// A & #oper，第7位移入C（立即型）
        /// </summary>
        [Instruction(false, 2, 2)]
        protected int Cmd0B()
        {
            byte op = AddressImmediate();
            Anc(op);
            return 2;
        }

        /// <summary>
        /// NOP
        /// 绝对型
        /// </summary>
        [Instruction(false, 3, 4)]
        protected int Cmd0C()
        {
            addr ad = AddressingAbsolute();
            Nop(ad);
            return 4;
        }

        /// <summary>
        /// ORA oper
        /// 逻辑或（绝对型）
        /// </summary>
        [Instruction(true, 3, 4)]
        protected int Cmd0D()
        {
            addr ad = AddressingAbsolute();
            Ora(ad);
            return 4;
        }

        /// <summary>
        /// ASL oper
        /// 算术左移(绝对型)
        /// </summary>
        [Instruction(true, 3, 6)]
        protected int Cmd0E()
        {
            addr ad = AddressingAbsolute();
            Asl(ad);
            return 6;
        }

        /// <summary>
        /// SLO $1234
        /// 算术左移内容一位并与A比较，结果存入A（绝对型）
        /// </summary>
        [Instruction(false, 3, 6)]
        protected int Cmd0F()
        {
            addr ad = AddressingAbsolute();
            Slo(ad);
            return 6;
        }

        #endregion

        #region ==============================| 6502 CPU 指令 1x |==============================

        /// <summary>
        /// BPL oper
        /// N=0 分支
        /// </summary>
        [Instruction(true, 2, 2, true, true)]
        protected int Cmd10()
        {
            int cycles = 2;
            addr ad = AddressingRelative();
            bool ps = (ad & 0xFF00) == (PC & 0xFF00);
            Bpl(ad);
            if (PC == ad)
            {
                cycles++;
                if (!ps) cycles++;
            }
            return cycles;
        }

        /// <summary>
        /// ORA (oper), Y
        /// 逻辑或（间接变址型）
        /// </summary>
        [Instruction(true, 2, 5, true)]
        protected int Cmd11()
        {
            addr ad = AddressingIndirectIndexed();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            Ora(ad);
            return 5 + (ps ? 0 : 1);
        }

        /// <summary>
        /// KIL
        /// </summary>
        [Instruction(false, 1, 0)]
        protected int Cmd12()
        {
            Kil();
            return 0;
        }

        /// <summary>
        /// SLO (oper), Y
        /// 算术左移内容一位并与A比较，结果存入A（间接变址型）
        /// </summary>
        [Instruction(false, 2, 8)]
        protected int Cmd13()
        {
            addr ad = AddressingIndirectIndexed();
            Slo(ad);
            return 8;
        }

        /// <summary>
        /// Nop
        /// 零页X变址
        /// </summary>
        [Instruction(false, 2, 4)]
        protected int Cmd14()
        {
            addr ad = AddressingZeroPageX();
            Nop(ad);
            return 4;
        }

        /// <summary>
        /// ORA oper, X
        /// 逻辑或（零页X变址）
        /// </summary>
        [Instruction(true, 2, 4)]
        protected int Cmd15()
        {
            addr ad = AddressingZeroPageX();
            Ora(ad);
            return 4;
        }

        /// <summary>
        /// ASL oper, X
        /// 算术左移（零页X变址）
        /// </summary>
        [Instruction(true, 2, 6)]
        protected int Cmd16()
        {
            addr ad = AddressingZeroPageX();
            Asl(ad);
            return 6;
        }

        /// <summary>
        /// SLO oper, x
        /// 算术左移内容一位并与A比较，结果存入A（零页X）
        /// </summary>
        [Instruction(false, 2, 6)]
        protected int Cmd17()
        {
            addr ad = AddressingZeroPageX();
            Slo(ad);
            return 6;
        }

        /// <summary>
        /// CLC
        /// 清进位标志
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int Cmd18()
        {
            Clc();
            return 2;
        }

        /// <summary>
        /// ORA oper, Y
        /// 逻辑或（绝对Y变址）
        /// </summary>
        [Instruction(true, 3, 4, true)]
        protected int Cmd19()
        {
            addr ad = AddressingAbsoluteY();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            Ora(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// NOP
        /// </summary>
        [Instruction(false, 1, 2)]
        protected int Cmd1A()
        {
            Nop();
            return 2;
        }


        /// <summary>
        /// SLO $1234, X
        /// 算术左移内容一位并与A比较，结果存入A（绝对Y变址）
        /// </summary>
        [Instruction(false, 3, 7)]
        protected int Cmd1B()
        {
            addr ad = AddressingAbsoluteY();
            Slo(ad);
            return 7;
        }

        /// <summary>
        /// Nop
        /// 绝对X变址
        /// </summary>
        [Instruction(false, 3, 4, true)]
        protected int Cmd1C()
        {
            addr ad = AddressingAbsoluteX();
            bool ps = ((ad - X) & 0xFF00) == (ad & 0xFF00);
            Nop(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// ORA oper, X
        /// 逻辑或（绝对X变址）
        /// </summary>
        [Instruction(true, 3, 4, true)]
        protected int Cmd1D()
        {
            addr ad = AddressingAbsoluteX();
            bool ps = ((ad - X) & 0xFF00) == (ad & 0xFF00);
            Ora(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// ASL oper, X
        /// 算术左移（绝对X变址）
        /// </summary>
        [Instruction(true, 3, 7)]
        protected int Cmd1E()
        {
            addr ad = AddressingAbsoluteX();
            Asl(ad);
            return 7;
        }

        /// <summary>
        /// SLO oper, X
        /// 算术左移内容一位并与A比较，结果存入A（绝对X变址）
        /// </summary>
        [Instruction(false, 3, 7)]
        protected int Cmd1F()
        {
            addr ad = AddressingAbsoluteX();
            Slo(ad);
            return 7;
        }

        #endregion

        #region ==============================| 6502 CPU 指令 2x |==============================

        /// <summary>
        /// JSR oper 
        /// 跳转至子程序（绝对型）
        /// </summary>
        [Instruction(true, 3, 6)]
        protected int Cmd20()
        {
            addr ad = AddressingAbsolute();
            Jsr(ad);
            return 6;
        }

        /// <summary>
        /// AND (oper, X)
        /// 累加器逻辑与(索引间接型)
        /// </summary>
        [Instruction(true, 2, 6)]
        protected int Cmd21()
        {
            addr ad = AddressingIndexedIndirect();
            And(ad);
            return 6;
        }

        /// <summary>
        /// KIL
        /// </summary>
        [Instruction(false, 1, 1)]
        protected int Cmd22()
        {
            Kil();
            return 0;
        }

        /// <summary>
        /// RLA (oper, X)
        /// 内存值循环右移1位、并于累加器比较（索引间接型）
        /// </summary>
        [Instruction(false, 1, 8)]
        protected int Cmd23()
        {
            addr ad = AddressingIndexedIndirect();
            Rla(ad);
            return 8;
        }

        /// <summary>
        /// BIT oper
        /// 位测试（零页）
        /// </summary>
        [Instruction(true, 2, 3)]
        protected int Cmd24()
        {
            addr ad = AddressingZeroPage();
            Bit(ad);
            return 3;
        }

        /// <summary>
        /// AND oper
        /// 累加器逻辑与（零页）
        /// </summary>
        [Instruction(true, 2, 3)]
        protected int Cmd25()
        {
            addr ad = AddressingZeroPage();
            And(ad);
            return 3;
        }

        /// <summary>
        /// ROL oper
        /// 循环左移（零页）
        /// </summary>
        [Instruction(true, 2, 5)]
        protected int Cmd26()
        {
            addr ad = AddressingZeroPage();
            Rol(ad);
            return 5;
        }

        /// <summary>
        /// RLA oper
        /// 内存值循环右移1位、并于累加器比较（零页）
        /// </summary>
        [Instruction(false, 2, 5)]
        protected int Cmd27()
        {
            addr ad = AddressingZeroPage();
            Rla(ad);
            return 5;
        }

        /// <summary>
        /// PLP
        /// 处理器状态出栈
        /// </summary>
        [Instruction(true, 1, 4)]
        protected int Cmd28()
        {
            Plp();
            return 4;
        }

        /// <summary>
        /// AND #oper
        /// 累加器逻辑与（立即型）
        /// </summary>
        [Instruction(true, 2, 2)]
        protected int Cmd29()
        {
            byte op = AddressImmediate();
            And(op);
            return 2;
        }

        /// <summary>
        /// ROL
        /// 循环左移（累加器）
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int Cmd2A()
        {
            Rol();
            return 2;
        }

        /// <summary>
        /// ANC #oper
        /// A & oper, 第7位送C
        /// </summary>
        [Instruction(false, 2, 2)]
        protected int Cmd2B()
        {
            byte op = AddressImmediate();
            Anc(op);
            return 2;
        }

        /// <summary>
        /// BIT oper
        /// 位测试（绝对型）
        /// </summary>
        [Instruction(true, 3, 4)]
        protected int Cmd2C()
        {
            addr ad = AddressingAbsolute();
            Bit(ad);
            return 4;
        }

        /// <summary>
        /// AND oper
        /// 累加器逻辑与（绝对型寻址）
        /// </summary>
        [Instruction(true, 3, 4)]
        protected int Cmd2D()
        {
            addr ad = AddressingAbsolute();
            And(ad);
            return 4;
        }

        /// <summary>
        /// ROL oper
        /// 循环左移（绝对型）
        /// </summary>
        [Instruction(true, 3, 6)]
        protected int Cmd2E()
        {

            addr ad = AddressingAbsolute();
            Rol(ad);
            return 6;
        }

        /// <summary>
        /// RLA oper
        /// 内存值循环右移1位、并于累加器比较（绝对型）
        /// </summary>
        [Instruction(false, 3, 6)]
        protected int Cmd2F()
        {
            addr ad = AddressingAbsolute();
            Rla(ad);
            return 6;
        }

        #endregion

        #region ==============================| 6502 CPU 指令 3x |==============================

        /// <summary>
        /// BMI oper
        /// N=1 分支
        /// </summary>
        [Instruction(true, 2, 2, true, true)]
        protected int Cmd30()
        {
            int cycles = 2;
            addr ad = AddressingRelative();
            bool ps = (ad & 0xFF00) == (PC & 0xFF00);
            Bmi(ad);
            if (PC == ad)
            {
                cycles++;
                if (!ps) cycles++;
            }
            return cycles;
        }

        /// <summary>
        /// AND (oper), Y
        /// 累加器逻辑与（间接变址型）
        /// </summary>
        [Instruction(true, 2, 5, true)]
        protected int Cmd31()
        {
            addr ad = AddressingIndirectIndexed();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            And(ad);
            return 5 + (ps ? 0 : 1);
        }

        /// <summary>
        /// KIL
        /// </summary>
        [Instruction(false, 1, 1)]
        protected int Cmd32()
        {
            Kil();
            return 0;
        }

        /// <summary>
        /// RLA (oper), Y
        /// 内存值循环右移1位、并于累加器比较（间接变址型 ）
        /// </summary>
        [Instruction(false, 2, 8)]
        protected int Cmd33()
        {
            addr ad = AddressingIndirectIndexed();
            Rla(ad);
            return 8;
        }

        /// <summary>
        /// Nop
        /// 零页X变址
        /// </summary>
        [Instruction(false, 2, 4)]
        protected int Cmd34()
        {
            addr ad = AddressingZeroPageX();
            Nop(ad);
            return 4;
        }

        /// <summary>
        /// AND oper, X
        /// 累加器逻辑与（零页X变址）
        /// </summary>
        [Instruction(true, 2, 4)]
        protected int Cmd35()
        {
            addr ad = AddressingZeroPageX();
            And(ad);
            return 4;
        }

        /// <summary>
        /// ROL oper, X
        /// 循环左移（零页X变址）
        /// </summary>
        [Instruction(true, 2, 6)]
        protected int Cmd36()
        {
            addr ad = AddressingZeroPageX();
            Rol(ad);
            return 6;
        }

        /// <summary>
        /// RLA oper, X
        /// 内存值循环右移1位、并于累加器比较（零页X变址）
        /// </summary>]
        [Instruction(false, 2, 6)]
        protected int Cmd37()
        {
            addr ad = AddressingZeroPageX();
            Rla(ad);
            return 6;
        }

        /// <summary>
        /// SEC
        /// 置进位标志
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int Cmd38()
        {
            Sec();
            return 2;
        }

        /// <summary>
        /// AND oper, Y
        /// 累加器逻辑与（绝对Y变址）
        /// </summary>
        [Instruction(true, 3, 4, true)]
        protected int Cmd39()
        {
            addr ad = AddressingAbsoluteY();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            And(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// NOP
        /// </summary>
        [Instruction(false, 1, 2)]
        protected int Cmd3A()
        {
            Nop();
            return 2;
        }

        /// <summary>
        /// RLA oper, Y
        /// 内存值循环右移1位、并于累加器比较（绝对Y变址）
        /// </summary>
        [Instruction(false, 3, 7)]
        protected int Cmd3B()
        {
            addr ad = AddressingAbsoluteY();
            Rla(ad);
            return 7;
        }

        /// <summary>
        /// Nop
        /// 绝对X变址
        /// </summary>
        [Instruction(false, 3, 4, true)]
        protected int Cmd3C()
        {
            addr ad = AddressingAbsoluteX();
            bool ps = ((ad - X) & 0xFF00) == (ad & 0xFF00);
            Nop(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// AND oper, X
        /// 累加器逻辑与（绝对X变址）
        /// </summary>
        [Instruction(true, 3, 4, true)]
        protected int Cmd3D()
        {
            addr ad = AddressingAbsoluteX();
            bool ps = ((ad - X) & 0xFF00) == (ad & 0xFF00);
            And(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// ROL oper, X
        /// 循环左移（绝对X变址）
        /// </summary>
        [Instruction(true, 3, 7)]
        protected int Cmd3E()
        {
            addr ad = AddressingAbsoluteX();
            Rol(ad);
            return 7;
        }

        /// <summary>
        /// RLA oper, X
        /// 内存值循环右移1位、并于累加器比较（绝对X变址）
        /// </summary>
        [Instruction(false, 3, 7)]
        protected int Cmd3F()
        {
            addr ad = AddressingAbsoluteX();
            Rla(ad);
            return 7;
        }

        #endregion

        #region ==============================| 6502 CPU 指令 4x |==============================

        /// <summary>
        /// RTI 中断返回
        /// </summary>
        [Instruction(true, 1, 6)]
        protected int Cmd40()
        {
            Rti();
            return 6;
        }

        /// <summary>
        /// EOR (oper, X)
        /// 累加器逻辑异或(变址间接)
        /// </summary>
        [Instruction(true, 2, 6)]
        protected int Cmd41()
        {
            addr ad = AddressingIndexedIndirect();
            Eor(ad);
            return 6;
        }

        /// <summary>
        /// KIL
        /// </summary>
        [Instruction(false, 1, 1)]
        protected int Cmd42()
        {
            Kil();
            return 0;
        }

        /// <summary>
        /// SRE (oper, X)
        /// LSR + EOR（索引间接型）
        /// </summary>
        [Instruction(false, 2, 8)]
        protected int Cmd43()
        {
            addr ad = AddressingIndexedIndirect();
            Sre(ad);
            return 8;
        }

        /// <summary>
        /// NOP oper
        /// 空操作(零页)
        /// </summary>
        [Instruction(false, 2, 3)]
        protected int Cmd44()
        {
            addr ad = AddressingZeroPage();
            Nop(ad);
            return 3;
        }

        /// <summary>
        /// EOR oper
        /// 累加器逻辑异或（零页）
        /// </summary>
        [Instruction(true, 2, 3)]
        protected int Cmd45()
        {
            addr ad = AddressingZeroPage();
            Eor(ad);
            return 3;
        }

        /// <summary>
        /// LSR oper
        /// 算术右移（零页）
        /// </summary>
        [Instruction(true, 2, 5)]
        protected int Cmd46()
        {
            addr ad = AddressingZeroPage();
            Lsr(ad);
            return 5;
        }

        /// <summary>
        /// SRE oper
        /// LSR + EOR（零页）
        /// </summary>
        [Instruction(false, 2, 5)]
        protected int Cmd47()
        {
            addr ad = AddressingZeroPage();
            Sre(ad);
            return 5; ;
        }

        /// <summary>
        /// PHA
        /// 累加器入栈
        /// </summary>
        [Instruction(true, 1, 3)]
        protected int Cmd48()
        {
            Pha();
            return 3;
        }

        /// <summary>
        /// EOR #oper
        /// 累加器逻辑异或（立即型）
        /// </summary>
        [Instruction(true, 2, 2)]
        protected int Cmd49()
        {
            byte op = AddressImmediate();
            Eor(op);
            return 2;
        }

        /// <summary>
        /// LSR
        /// 算术右移（累加器型）
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int Cmd4A()
        {
            Lsr();
            return 2;
        }

        /// <summary>
        /// ALR #oper
        /// </summary>
        [Instruction(false, 2, 2)]
        protected int Cmd4B()
        {
            byte op = AddressImmediate();
            Alr(op);
            return 2;
        }

        /// <summary>
        /// JMP oper
        /// 跳转（绝对型）
        /// </summary>
        [Instruction(true, 3, 3)]
        protected int Cmd4C()
        {

            addr ad = AddressingAbsolute();
            Jmp(ad);
            return 3;
        }

        /// <summary>
        /// EOR oper
        /// 累加器逻辑异或（绝对型）
        /// </summary>
        [Instruction(true, 3, 4)]
        protected int Cmd4D()
        {
            addr ad = AddressingAbsolute();
            Eor(ad);
            return 4;
        }

        /// <summary>
        /// LSR oper
        /// 算术右移（绝对型）
        /// </summary>
        [Instruction(true, 3, 6)]
        protected int Cmd4E()
        {
            addr ad = AddressingAbsolute();
            Lsr(ad);
            return 6;
        }

        /// <summary>
        /// SRE oper
        /// LSR + EOR（绝对型）
        /// </summary>
        [Instruction(false, 3, 6)]
        protected int Cmd4F()
        {
            addr ad = AddressingAbsolute();
            Sre(ad);
            return 6;
        }

        #endregion

        #region ==============================| 6502 CPU 指令 5x |==============================

        /// <summary>
        /// BVC oper
        /// V=0 分支
        /// </summary>
        [Instruction(true, 2, 2, true, true)]
        protected int Cmd50()
        {
            int cycles = 2;
            addr ad = AddressingRelative();
            bool ps = (ad & 0xFF00) == (PC & 0xFF00);
            Bvc(ad);
            if (PC == ad)
            {
                cycles++;
                if (!ps) cycles++;
            }
            return cycles;
        }

        /// <summary>
        /// EOR (oper), Y
        /// 累加器逻辑异或（间接变址型）
        /// </summary>
        [Instruction(true, 2, 5, true)]
        protected int Cmd51()
        {
            addr ad = AddressingIndirectIndexed();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            Eor(ad);
            return 5 + (ps ? 0 : 1);
        }

        /// <summary>
        /// KIL
        /// </summary>
        [Instruction(false, 1, 1)]
        protected int Cmd52()
        {
            Kil();
            return 0;
        }

        /// <summary>
        /// SRE (oper), Y
        /// LSR + EOR（间接变址型）
        /// </summary>
        [Instruction(false, 2, 8)]
        protected int Cmd53()
        {
            addr ad = AddressingIndirectIndexed();
            Sre(ad);
            return 8;
        }

        /// <summary>
        /// Nop
        /// 零页X变址
        /// </summary>
        [Instruction(false, 2, 4)]
        protected int Cmd54()
        {
            addr ad = AddressingZeroPageX();
            Nop(ad);
            return 4;
        }

        /// <summary>
        /// EOR oper, X
        /// 累加器逻辑异或（零页X变址）
        /// </summary>
        [Instruction(true, 2, 4)]
        protected int Cmd55()
        {
            addr ad = AddressingZeroPageX();
            Eor(ad);
            return 4;
        }

        /// <summary>
        /// LSR oper, X
        /// 算术右移（零页X变址）
        /// </summary>
        [Instruction(true, 2, 6)]
        protected int Cmd56()
        {
            addr ad = AddressingZeroPageX();
            Lsr(ad);
            return 6;
        }

        /// <summary>
        /// SRE oper, X
        /// LSR + EOR（零页X变址）
        /// </summary>
        [Instruction(false, 2, 6)]
        protected int Cmd57()
        {
            addr ad = AddressingZeroPageX();
            Sre(ad);
            return 6;
        }

        /// <summary>
        /// CLI
        /// 清中断
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int Cmd58()
        {
            Cli();
            return 2;
        }

        /// <summary>
        /// EOR oper, Y
        /// 累加器逻辑异或（绝对Y变址）
        /// </summary>
        [Instruction(true, 3, 4, true)]
        protected int Cmd59()
        {
            addr ad = AddressingAbsoluteY();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            Eor(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// Nop
        /// </summary>
        [Instruction(false, 1, 2)]
        protected int Cmd5A()
        {
            Nop();
            return 2;
        }

        /// <summary>
        /// SRE oper, Y
        /// LSR + EOR（绝对Y变址）
        /// </summary>
        [Instruction(false, 3, 7)]
        protected int Cmd5B()
        {
            addr ad = AddressingAbsoluteY();
            Sre(ad);
            return 7;
        }

        /// <summary>
        /// Nop
        /// 绝对X变址
        /// </summary>
        [Instruction(false, 3, 4, true)]
        protected int Cmd5C()
        {
            addr ad = AddressingAbsoluteX();
            bool ps = ((ad - X) & 0xFF00) == (ad & 0xFF00);
            Nop(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// EOR oper, X
        /// 累加器逻辑异或（绝对X变址）
        /// </summary>
        [Instruction(true, 3, 4, true)]
        protected int Cmd5D()
        {
            addr ad = AddressingAbsoluteX();
            bool ps = ((ad - X) & 0xFF00) == (ad & 0xFF00);
            Eor(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// LSR oper, X
        /// 算术右移（绝对X变址）
        /// </summary>
        [Instruction(true, 3, 7)]
        protected int Cmd5E()
        {
            addr ad = AddressingAbsoluteX();
            Lsr(ad);
            return 7;
        }

        /// <summary>
        /// SRE oper, X
        /// LSR + EOR（绝对X变址）
        /// </summary>
        [Instruction(false, 3, 7)]
        protected int Cmd5F()
        {
            addr ad = AddressingAbsoluteX();
            Sre(ad);
            return 7;
        }

        #endregion

        #region ==============================| 6502 CPU 指令 6x |==============================

        /// <summary>
        /// RTS 
        /// 子程序返回
        /// </summary>
        [Instruction(true, 1, 6)]
        protected int Cmd60()
        {
            Rts();
            return 6;
        }

        /// <summary>
        /// ADC (oper, X)
        /// 进位加法，A = A + 操作数 + C
        /// </summary>
        [Instruction(true, 2, 6)]
        protected int Cmd61()
        {
            addr ad = AddressingIndexedIndirect();
            Adc(ad);
            return 6;
        }

        /// <summary>
        /// KIL
        /// </summary
        [Instruction(false, 1, 1)]
        protected int Cmd62()
        {
            Kil();
            return 0;
        }

        /// <summary>
        /// RRA (oper, X)
        /// 内存值循环右移1位，并与累加器带进位相加（索引间接型）
        /// </summary>
        [Instruction(false, 2, 8)]
        protected int Cmd63()
        {
            addr ad = AddressingIndexedIndirect();
            Rra(ad);
            return 8;
        }

        /// <summary>
        /// NOP oper
        /// 空操作(零页)
        /// </summary>
        [Instruction(false, 2, 3)]
        protected int Cmd64()
        {
            addr ad = AddressingZeroPage();
            Nop(ad);
            return 3;
        }

        /// <summary>
        /// ADC oper
        /// 带进位加法（零页）
        /// </summary>
        [Instruction(true, 2, 3)]
        protected int Cmd65()
        {
            addr ad = AddressingZeroPage();
            Adc(ad);
            return 3;
        }

        /// <summary>
        /// ROR oper
        /// 循环右移（零页）
        /// </summary>
        [Instruction(true, 2, 5)]
        protected int Cmd66()
        {
            addr ad = AddressingZeroPage();
            Ror(ad);
            return 5;
        }

        /// <summary>
        /// RRA oper
        /// 内存值循环右移1位，并与累加器带进位相加（零页）
        /// </summary>
        [Instruction(false, 2, 5)]
        protected int Cmd67()
        {
            addr ad = AddressingZeroPage();
            Rra(ad);
            return 5;
        }

        /// <summary>
        /// PLA
        /// 累加器出栈
        /// </summary>
        [Instruction(true, 1, 4)]
        protected int Cmd68()
        {
            Pla();
            return 4;
        }

        /// <summary>
        /// ADC #oper
        /// 带进位加法（立即型）
        /// </summary>
        [Instruction(true, 2, 2)]
        protected int Cmd69()
        {
            byte op = AddressImmediate();
            Adc(op);
            return 2;
        }

        /// <summary>
        /// ROR
        /// 循环右移（累加器）
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int Cmd6A()
        {
            Ror();
            return 2;
        }

        /// <summary>
        /// ARR #oper
        /// （立即型）
        /// </summary>
        [Instruction(false, 2, 2)]
        protected int Cmd6B()
        {
            byte op = AddressImmediate();
            Arr(op);
            return 2;
        }

        /// <summary>
        /// JMP (oper)
        /// 跳转（间接型）
        /// </summary>
        [Instruction(true, 3, 5)]
        protected int Cmd6C()
        {
            addr ad = AddressingIndirect();
            Jmp(ad);
            return 5;
        }

        /// <summary>
        /// ADC oper
        /// 带进位加法（绝对型）
        /// </summary>
        [Instruction(true, 3, 4)]
        protected int Cmd6D()
        {
            addr ad = AddressingAbsolute();
            Adc(ad);
            return 4;
        }

        /// <summary>
        /// ROR oper
        /// 循环右移（绝对）
        /// </summary>
        [Instruction(true, 3, 6)]
        protected int Cmd6E()
        {
            addr ad = AddressingAbsolute();
            Ror(ad);
            return 6;
        }

        /// <summary>
        /// RRA oper
        /// 内存值循环右移1位，并与累加器带进位相加（绝对型）
        /// </summary>
        [Instruction(false, 3, 6)]
        protected int Cmd6F()
        {
            addr ad = AddressingAbsolute();
            Rra(ad);
            return 6;
        }

        #endregion

        #region ==============================| 6502 CPU 指令 7x |==============================

        /// <summary>
        /// BVS oper
        /// V=1 分支
        /// </summary>
        [Instruction(true, 2, 2, true, true)]
        protected int Cmd70()
        {
            int cycles = 2;
            addr ad = AddressingRelative();
            bool ps = (ad & 0xFF00) == (PC & 0xFF00);
            Bvs(ad);
            if (PC == ad)
            {
                cycles++;
                if (!ps) cycles++;
            }
            return cycles;
        }

        /// <summary>
        /// ADC (oper), Y
        /// 带进位加法（间接变址型）
        /// </summary>
        [Instruction(true, 2, 5, true)]
        protected int Cmd71()
        {
            addr ad = AddressingIndirectIndexed();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            Adc(ad);
            return 5 + (ps ? 0 : 1);
        }

        /// <summary>
        /// KIL
        /// </summary>
        [Instruction(false, 1, 1)]
        protected int Cmd72()
        {
            Kil();
            return 0;
        }

        /// <summary>
        /// RRA (oper),Y
        /// 内存值循环右移1位，并与累加器带进位相加（间接变址型）
        /// </summary>
        [Instruction(false, 2, 8)]
        protected int Cmd73()
        {
            addr ad = AddressingIndirectIndexed();
            Rra(ad);
            return 8;
        }

        /// <summary>
        /// Nop
        /// 零页X变址
        /// </summary>
        [Instruction(false, 2, 4)]
        protected int Cmd74()
        {
            addr ad = AddressingZeroPageX();
            Nop(ad);
            return 4;
        }

        /// <summary>
        /// ADC oper, X
        /// 带进位加法（零页X变址）
        /// </summary>
        [Instruction(true, 2, 4)]
        protected int Cmd75()
        {
            addr ad = AddressingZeroPageX();
            Adc(ad);
            return 4;
        }

        /// <summary>
        /// ROR oper, X
        /// 循环右移（零页X变址）
        /// </summary>
        [Instruction(true, 2, 6)]
        protected int Cmd76()
        {
            addr ad = AddressingZeroPageX();
            Ror(ad);
            return 6;
        }

        /// <summary>
        /// RRA oper, x
        /// 内存值循环右移1位，并与累加器带进位相加（零页X变址）
        /// </summary>
        [Instruction(false, 2, 6)]
        protected int Cmd77()
        {
            addr ad = AddressingZeroPageX();
            Rra(ad);
            return 6;
        }

        /// <summary>
        /// SEI
        /// 置中断标志
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int Cmd78()
        {
            Sei();
            return 2;
        }

        /// <summary>
        /// ADC oper Y
        /// 带进位加法（绝对Y变址）
        /// </summary>
        [Instruction(false, 3, 4, true)]
        protected int Cmd79()
        {
            addr ad = AddressingAbsoluteY();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            Adc(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// NOP
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int Cmd7A()
        {
            Nop();
            return 2;
        }

        /// <summary>
        /// RRA oper, Y
        /// 内存值循环右移1位，并与累加器带进位相加（绝对Y变址）
        /// </summary>
        [Instruction(false, 3, 7)]
        protected int Cmd7B()
        {
            addr ad = AddressingAbsoluteY();
            Rra(ad);
            return 7;
        }

        /// <summary>
        /// Nop
        /// 绝对X变址
        /// </summary>
        [Instruction(true, 3, 4, true)]
        protected int Cmd7C()
        {
            addr ad = AddressingAbsoluteX();
            bool ps = ((ad - X) & 0xFF00) == (ad & 0xFF00);
            Nop(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// ADC oper, X
        /// 带进位加法（绝对X变址）
        /// </summary>
        [Instruction(true, 3, 4, true)]
        protected int Cmd7D()
        {
            addr ad = AddressingAbsoluteX();
            bool ps = ((ad - X) & 0xFF00) == (ad & 0xFF00);
            Adc(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// ROL oper, X
        /// 循环右移（绝对X变址）
        /// </summary>
        [Instruction(true, 3, 7)]
        protected int Cmd7E()
        {
            addr ad = AddressingAbsoluteX();
            Ror(ad);
            return 7;
        }

        /// <summary>
        /// RRA oper, X
        /// 内存值循环右移1位，并与累加器带进位相加（绝对X变址）
        /// </summary>
        [Instruction(false, 3, 7)]
        protected int Cmd7F()
        {
            addr ad = AddressingAbsoluteX();
            Rra(ad);
            return 7;
        }

        #endregion

        #region ==============================| 6502 CPU 指令 8x |==============================

        /// <summary>
        /// NOP #oper
        /// 空操作
        /// </summary>
        [Instruction(true, 2, 2)]
        protected int Cmd80()
        {
            byte op = AddressImmediate();
            Nop(op);
            return 2;
        }

        /// <summary>
        /// STA (oper, X)
        /// 存累加器（索引间接型）。
        /// </summary>
        [Instruction(false, 2, 6)]
        protected int Cmd81()
        {
            addr ad = AddressingIndexedIndirect();
            Sta(ad);
            return 6;
        }

        /// <summary>
        /// NOP #oper
        /// 空操作
        /// </summary>
        [Instruction(false, 2, 2)]
        protected int Cmd82()
        {
            byte op = AddressImmediate();
            Nop(op);
            return 2;
        }

        /// <summary>
        /// SAX (oper, X)
        /// 将 A 和 X求逻辑与，并输出至总线，存到内存（索引间接型）
        /// 又名AXS、AAX
        /// </summary>
        [Instruction(false, 2, 6)]
        protected int Cmd83()
        {
            addr ad = AddressingIndexedIndirect();
            Sax(ad);
            return 6;
        }

        /// <summary>
        /// STY oper
        /// 存变址寄存器Y(零页)
        /// </summary>
        [Instruction(true, 2, 3)]
        protected int Cmd84()
        {
            addr ad = AddressingZeroPage();
            Sty(ad);
            return 3;
        }

        /// <summary>
        /// STA oper
        /// 存累加器（零页）
        /// </summary>
        [Instruction(true, 2, 3)]
        protected int Cmd85()
        {
            addr ad = AddressingZeroPage();
            Sta(ad);
            return 3;
        }

        /// <summary>
        /// STX oper
        /// 存变址寄存器X(零页)
        /// </summary>
        [Instruction(true, 2, 3)]
        protected int Cmd86()
        {
            addr ad = AddressingZeroPage();
            Stx(ad);
            return 3;
        }

        /// <summary>
        /// SAX oper
        /// 将 A 和 X求逻辑与，并输出至总线，存到内存（零页）
        /// 又名AXS、AAX
        /// </summary>
        [Instruction(false, 2, 3)]
        protected int Cmd87()
        {
            addr ad = AddressingZeroPage();
            Sax(ad);
            return 3;
        }

        /// <summary>
        /// DEY
        /// 变址寄存器Y减1
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int Cmd88()
        {
            Dey();
            return 2;
        }

        /// <summary>
        /// NOP #oper
        /// 空操作
        /// </summary>
        [Instruction(false, 2, 2)]
        protected int Cmd89()
        {
            byte op = AddressImmediate();
            Nop(op);
            return 2;
        }

        /// <summary>
        /// TXA
        /// 变址寄存器X送累加器
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int Cmd8A()
        {
            Txa();
            return 2;
        }

        /// <summary>
        /// XAA #oper
        /// （立即型）
        /// </summary>
        [Instruction(false, 2, 2)]
        protected int Cmd8B()
        {
            byte op = AddressImmediate();
            Xaa(op);
            return 2;
        }

        /// <summary>
        /// STY oper
        /// 存变址寄存器Y（绝对型）
        /// </summary>
        [Instruction(true, 3, 4)]
        protected int Cmd8C()
        {
            addr ad = AddressingAbsolute();
            Sty(ad);
            return 4;
        }

        /// <summary>
        /// STA oper
        /// 存累加器（绝对型）
        /// </summary>
        [Instruction(true, 3, 4)]
        protected int Cmd8D()
        {
            addr ad = AddressingAbsolute();
            Sta(ad);
            return 4;
        }

        /// <summary>
        /// STX oper
        /// 存变址寄存器X（绝对型）
        /// </summary>
        [Instruction(true, 3, 4)]
        protected int Cmd8E()
        {
            addr ad = AddressingAbsolute();
            Stx(ad);
            return 4;
        }

        /// <summary>
        /// SAX oper
        /// 将 A 和 X求逻辑与，并输出至总线，存到内存（绝对型）
        /// 又名AXS、AAX
        /// </summary>
        [Instruction(false, 3, 4)]
        protected int Cmd8F()
        {
            addr ad = AddressingAbsolute();
            Sax(ad);
            return 4;
        }

        #endregion

        #region ==============================| 6502 CPU 指令 9x |==============================

        /// <summary>
        /// BCC oper
        /// C=0 分支
        /// </summary>
        [Instruction(true, 2, 2, true, true)]
        protected int Cmd90()
        {
            int cycles = 2;
            addr ad = AddressingRelative();
            bool ps = (ad & 0xFF00) == (PC & 0xFF00);
            Bcc(ad);
            if (PC == ad)
            {
                cycles++;
                if (!ps)
                    cycles++;
            }
            return cycles;
        }

        /// <summary>
        /// STA (oper), Y
        /// 存累加器（间接变址型）
        /// </summary>
        [Instruction(true, 2, 6)]
        protected int Cmd91()
        {
            addr ad = AddressingIndirectIndexed();
            Sta(ad);
            return 6;
        }

        /// <summary>
        /// KIL
        /// </summary>
        [Instruction(false, 1, 1)]
        protected int Cmd92()
        {
            Kil();
            return 0;
        }

        /// <summary>
        /// SHA oper, Y
        /// 将 A & X & H 存入内存（间接变址）
        /// 又名AHX, AXA
        /// </summary>
        [Instruction(false, 2, 6)]
        protected int Cmd93()
        {
            addr ad = AddressingIndirectIndexed();
            Ahx(ad);
            return 6;
        }

        /// <summary>
        /// STY oper, X
        /// 存变址寄存器Y(零页X变址)
        /// </summary>
        [Instruction(true, 2, 4)]
        protected int Cmd94()
        {
            addr ad = AddressingZeroPageX();
            Sty(ad);
            return 4;
        }

        /// <summary>
        /// STA oper, X
        /// 存累加器（零页X变址）
        /// </summary>
        [Instruction(true, 2, 4)]
        protected int Cmd95()
        {
            addr ad = AddressingZeroPageX();
            Sta(ad);
            return 4;
        }

        /// <summary>
        /// STX oper, Y
        /// 存变址寄存器X（零页Y变址）
        /// </summary>
        [Instruction(true, 2, 4)]
        protected int Cmd96()
        {
            addr ad = AddressingZeroPageY();
            Stx(ad);
            return 4;
        }

        /// <summary>
        /// SAX oper, Y
        /// 将 A 和 X求逻辑与，并输出至总线，存到内存（零页Y变址）
        /// 又名AXS、AAX
        /// </summary>
        [Instruction(false, 2, 4)]
        protected int Cmd97()
        {
            addr ad = AddressingZeroPageY();
            Sax(ad);
            return 4;
        }

        /// <summary>
        /// TYA
        /// 变址寄存器Y送累加器
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int Cmd98()
        {
            Tya();
            return 2;
        }

        /// <summary>
        /// STA oper, Y
        /// 存累加器（绝对Y变址）
        /// </summary>
        [Instruction(true, 3, 5)]
        protected int Cmd99()
        {
            addr ad = AddressingAbsoluteY();
            Sta(ad);
            return 5;
        }

        /// <summary>
        /// TXS
        /// 变址寄存器X送栈指针
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int Cmd9A()
        {
            Txs();
            return 2;
        }

        /// <summary>
        /// TAS oper, Y
        /// 存 A & X 到SP, A & X & H 到内存（绝对Y变址）
        /// 别名：XAS、SHS
        /// </summary>
        [Instruction(false, 3, 5)]
        protected int Cmd9B()
        {
            addr ad = AddressingAbsoluteY();
            Tas(ad);
            return 5;
        }

        /// <summary>
        /// SHY oper, X
        /// 将 Y & H 存入内存
        /// 别名：A11, SYA, SAY
        /// </summary>
        [Instruction(false, 3, 5)]
        protected int Cmd9C()
        {
            addr ad = AddressingAbsoluteY();
            Shy(ad);
            return 5;
        }

        /// <summary>
        /// STA oper, X
        /// 存累加器（绝对X变址）
        /// </summary>
        [Instruction(true, 3, 5)]
        protected int Cmd9D()
        {
            addr ad = AddressingAbsoluteX();
            Sta(ad);
            return 5;
        }

        /// <summary>
        /// SHX oper, Y
        /// 将 X & H 存入内存（绝对Y变址）
        /// 别名：A11, SXA, XAS
        /// </summary>
        [Instruction(false, 3, 5)]
        protected int Cmd9E()
        {
            addr ad = AddressingAbsoluteY();
            Shx(ad);
            return 5;
        }

        /// <summary>
        /// SHA oper, Y
        /// 将 A & X & H 存入内存（绝对Y变址）
        /// 又名AHX, AXA
        /// </summary>
        [Instruction(false, 3, 5)]
        protected int Cmd9F()
        {
            addr ad = AddressingAbsoluteY();
            Ahx(ad);
            return 5;
        }

        #endregion

        #region ==============================| 6502 CPU 指令 Ax |==============================

        /// <summary>
        /// LDY #oper
        /// 送变址寄存器Y（立即型）
        /// </summary>
        [Instruction(true, 2, 2)]
        protected int CmdA0()
        {
            byte op = AddressImmediate();
            Ldy(op);
            return 2;
        }

        /// <summary>
        /// LDA (oper, X)
        /// 从内存中读出累加器A(索引间接型)。
        /// </summary>
        [Instruction(true, 2, 6)]
        protected int CmdA1()
        {
            addr ad = AddressingIndexedIndirect();
            Lda(ad);
            return 6;
        }

        /// <summary>
        /// LDX #oper
        /// 送变址寄存器X(立即型)
        /// </summary>
        [Instruction(true, 2, 2)]
        protected int CmdA2()
        {
            byte op = AddressImmediate();
            Ldx(op);
            return 2;
        }

        /// <summary>
        /// LAX (oper, X)
        /// （索引间接型）
        /// </summary>
        [Instruction(false, 2, 6)]
        protected int CmdA3()
        {
            addr ad = AddressingIndexedIndirect();
            Lax(ad);
            return 6;
        }

        /// <summary>
        /// LDY oper
        /// 取变址寄存器Y（零页）
        /// </summary>
        [Instruction(true, 2, 3)]
        protected int CmdA4()
        {
            addr ad = AddressingZeroPage();
            Ldy(ad);
            return 3;
        }

        /// <summary>
        /// LDA oper
        /// 送累加器（零页）
        /// </summary>
        [Instruction(true, 2, 3)]
        protected int CmdA5()
        {
            addr ad = AddressingZeroPage();
            Lda(ad);
            return 3;
        }

        /// <summary>
        /// LDX oper
        /// 送变址寄存器X（零页）
        /// </summary>
        [Instruction(true, 2, 3)]
        protected int CmdA6()
        {
            addr ad = AddressingZeroPage();
            Ldx(ad);
            return 3;
        }

        /// <summary>
        /// LAX oper
        /// （零页）
        /// </summary>
        [Instruction(false, 2, 3)]
        protected int CmdA7()
        {
            addr ad = AddressingZeroPage();
            Lax(ad);
            return 3;
        }

        /// <summary>
        /// TAY
        /// 累加器送变址寄存器Y
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int CmdA8()
        {
            Tay();
            return 2;
        }

        /// <summary>
        /// LDA #oper
        /// 送累加器（立即型）
        /// </summary>
        [Instruction(true, 2, 2)]
        protected int CmdA9()
        {
            byte op = AddressImmediate();
            Lda(op);
            return 2;
        }

        /// <summary>
        /// TAX
        /// 累加器送变址寄存器X
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int CmdAA()
        {
            Tax();
            return 2;
        }

        /// <summary>
        /// LXA
        /// </summary>
        [Instruction(false, 2, 2)]
        protected int CmdAB()
        {
            byte op = AddressImmediate();
            Lax(op);
            return 2;
        }

        /// <summary>
        /// LDY oper
        /// 送变址寄存器Y(绝对型)
        /// </summary>
        [Instruction(true, 3, 4)]
        protected int CmdAC()
        {
            addr ad = AddressingAbsolute();
            Ldy(ad);
            return 4;
        }

        /// <summary>
        /// LDA oper
        /// 送累加器（绝对型）
        /// </summary>
        [Instruction(true, 3, 4)]
        protected int CmdAD()
        {
            addr ad = AddressingAbsolute();
            Lda(ad);
            return 4;
        }

        /// <summary>
        /// LDX oper
        /// 送变址寄存器X(绝对型)
        /// </summary>
        [Instruction(true, 3, 4)]
        protected int CmdAE()
        {
            addr ad = AddressingAbsolute();
            Ldx(ad);
            return 4;
        }

        /// <summary>
        /// LAX oper
        /// （绝对型）
        /// </summary>
        [Instruction(false, 3, 4)]
        protected int CmdAF()
        {
            addr ad = AddressingAbsolute();
            Lax(ad);
            return 4;
        }

        #endregion

        #region ==============================| 6502 CPU 指令 Bx |==============================

        /// <summary>
        /// BCS oper
        /// C=1 分支
        /// </summary>
        [Instruction(true, 2, 2, true, true)]
        protected int CmdB0()
        {
            int cycles = 2;
            addr ad = AddressingRelative();
            bool ps = (ad & 0xFF00) == (PC & 0xFF00);
            Bcs(ad);
            if (PC == ad)
            {
                cycles++;
                if (!ps)
                    cycles++;
            }
            return cycles;
        }

        /// <summary>
        /// LDA (oper), Y
        /// 送累加器（间接变址型）
        /// </summary>
        [Instruction(true, 2, 5, true)]
        protected int CmdB1()
        {
            addr ad = AddressingIndirectIndexed();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            Lda(ad);
            return 5 + (ps ? 0 : 1);
        }

        /// <summary>
        /// KIL
        /// </summary>
        [Instruction(false, 1, 1)]
        protected int CmdB2()
        {
            Kil();
            return 0;
        }

        /// <summary>
        /// LAX (oper), Y
        /// （间接变址型 ）
        /// </summary>
        [Instruction(false, 2, 5, true)]
        protected int CmdB3()
        {
            addr ad = AddressingIndirectIndexed();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            Lax(ad);
            return 5 + (ps ? 0 : 1);
        }

        /// <summary>
        /// LDY oper, X
        /// 送变址寄存器Y(零页X变址)
        /// </summary>
        [Instruction(true, 2, 4)]
        protected int CmdB4()
        {
            addr ad = AddressingZeroPageX();
            Ldy(ad);
            return 4;
        }

        /// <summary>
        /// LDA oper, x
        /// 送累加器（零页X变址）
        /// </summary>
        [Instruction(true, 2, 4)]
        protected int CmdB5()
        {
            addr ad = AddressingZeroPageX();
            Lda(ad);
            return 4;
        }

        /// <summary>
        /// LDX oper, y
        /// 送变址寄存器X（零页Y变址）
        /// </summary>
        [Instruction(true, 2, 4)]
        protected int CmdB6()
        {
            addr ad = AddressingZeroPageY();
            Ldx(ad);
            return 4;
        }

        /// <summary>
        /// LAX oper, Y
        /// （零页Y变址）
        /// </summary>
        [Instruction(false, 2, 4)]
        protected int CmdB7()
        {
            addr ad = AddressingZeroPageY();
            Lax(ad);
            return 4;
        }

        /// <summary>
        /// CLV
        /// 清溢出标志
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int CmdB8()
        {
            Clv();
            return 2;
        }

        /// <summary>
        /// LDA oper, Y
        /// 送累加器（绝对Y变址）
        /// </summary>
        [Instruction(true, 3, 4, true)]
        protected int CmdB9()
        {
            addr ad = AddressingAbsoluteY();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            Lda(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// TSX
        /// 栈指针送变址寄存器X
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int CmdBA()
        {
            Tsx();
            return 2;
        }

        /// <summary>
        /// LAS oper, Y
        /// （绝对Y变址）
        /// </summary>
        [Instruction(false, 3, 4, true)]
        protected int CmdBB()
        {
            addr ad = AddressingAbsoluteY();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            Las(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// LDY oper, X
        /// 送变址寄存器Y(绝对X变址)
        /// </summary>
        [Instruction(true, 3, 4, true)]
        protected int CmdBC()
        {
            addr ad = AddressingAbsoluteX();
            bool ps = ((ad - X) & 0xFF00) == (ad & 0xFF00);
            Ldy(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// LDA oper, X
        /// 送累加器（绝对X变址）
        /// </summary>
        [Instruction(true, 3, 4, true)]
        protected int CmdBD()
        {
            addr ad = AddressingAbsoluteX();
            bool ps = ((ad - X) & 0xFF00) == (ad & 0xFF00);
            Lda(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// LDX oper, Y
        /// 送变址寄存器X（绝对Y变址）
        /// </summary>
        [Instruction(true, 3, 4, true)]
        protected int CmdBE()
        {
            addr ad = AddressingAbsoluteY();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            Ldx(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// LAX oper, Y
        /// （绝对Y变址）
        /// </summary>
        [Instruction(false, 3, 4, true)]
        protected int CmdBF()
        {
            addr ad = AddressingAbsoluteY();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            Lax(ad);
            return 4 + (ps ? 0 : 1);
        }

        #endregion

        #region ==============================| 6502 CPU 指令 Cx |==============================

        /// <summary>
        /// CPY #oper
        /// 变址寄存器Y与立即数比较
        /// </summary>
        [Instruction(true, 2, 2)]
        protected int CmdC0()
        {
            byte op = AddressImmediate();
            Cpy(op);
            return 2;
        }

        /// <summary>
        /// CMP (oper, X)
        /// 累加器A - 内存值，结果不保存。
        /// 索引间接型寻址，操作数(16位)放在 (oper)+X 和 (oper)+X+1中
        /// </summary>
        [Instruction(true, 2, 6)]
        protected int CmdC1()
        {
            addr ad = AddressingIndexedIndirect();
            Cmp(ad);
            return 6;
        }

        /// <summary>
        /// NOP #oper
        /// 空操作
        /// </summary>
        [Instruction(false, 2, 2)]
        protected int CmdC2()
        {
            byte op = AddressImmediate();
            Nop(op);
            return 2;
        }

        /// <summary>
        /// DCP (oper, X)
        /// （索引间接型）
        /// </summary>
        [Instruction(false, 2, 8)]
        protected int CmdC3()
        {
            addr ad = AddressingIndexedIndirect();
            Dcp(ad);
            return 8;
        }

        /// <summary>
        /// CPY oper
        /// 变址寄存器与内存比较（零页）
        /// </summary>
        [Instruction(true, 2, 3)]
        protected int CmdC4()
        {
            addr ad = AddressingZeroPage();
            Cpy(ad);
            return 3;
        }

        /// <summary>
        /// CMP oper
        /// 累加器与内存比较（零页）
        /// </summary>
        [Instruction(true, 2, 3)]
        protected int CmdC5()
        {
            addr ad = AddressingZeroPage();
            Cmp(ad);
            return 3;
        }

        /// <summary>
        /// DEC oper
        /// 存储器减1（零页）
        /// </summary>
        [Instruction(true, 2, 5)]
        protected int CmdC6()
        {
            addr ad = AddressingZeroPage();
            Dec(ad);
            return 5;
        }

        /// <summary>
        /// DCP oper
        /// （零页）
        /// </summary>
        [Instruction(false, 2, 5)]
        protected int CmdC7()
        {
            addr ad = AddressingZeroPage();
            Dcp(ad);
            return 5;
        }

        /// <summary>
        /// INY
        /// 变址寄存器Y加1
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int CmdC8()
        {
            Iny();
            return 2;
        }

        /// <summary>
        /// CMP #oper
        /// 累加器比较（立即型）
        /// </summary>
        [Instruction(true, 2, 2)]
        protected int CmdC9()
        {
            byte op = AddressImmediate();
            Cmp(op);
            return 2;
        }

        /// <summary>
        /// DEX
        /// 变址寄存器X减1
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int CmdCA()
        {
            Dex();
            return 2;
        }

        /// <summary>
        /// SBX #oper
        /// A 与 X 逻辑与，结果减去立即数后存入 X
        /// 别名：AXS, SAX
        /// </summary>
        [Instruction(false, 2, 2)]
        protected int CmdCB()
        {
            byte op = AddressImmediate();
            Axs(op);
            return 2;
        }

        /// <summary>
        /// CPY oper
        /// 变址寄存器Y与存储器比较(绝对型)
        /// </summary>
        [Instruction(true, 3, 4)]
        protected int CmdCC()
        {
            addr ad = AddressingAbsolute();
            Cpy(ad);
            return 4;
        }

        /// <summary>
        /// CMP oper
        /// 累加器与内存比较（绝对型）
        /// </summary>
        [Instruction(true, 3, 4)]
        protected int CmdCD()
        {
            addr ad = AddressingAbsolute();
            Cmp(ad);
            return 4;
        }

        /// <summary>
        /// DEC oper
        /// 存储器减1（绝对型）
        /// </summary>
        [Instruction(true, 3, 6)]
        protected int CmdCE()
        {
            addr ad = AddressingAbsolute();
            Dec(ad);
            return 6;
        }

        /// <summary>
        /// DCP oper
        /// （绝对型）
        /// </summary>
        [Instruction(false, 3, 6)]
        protected int CmdCF()
        {
            addr ad = AddressingAbsolute();
            Dcp(ad);
            return 6;
        }

        #endregion

        #region ==============================| 6502 CPU 指令 Dx |==============================

        /// <summary>
        /// BNE oper
        /// Z=0 分支
        /// </summary>
        [Instruction(true, 2, 2, true, true)]
        protected int CmdD0()
        {
            int cycles = 2;
            addr ad = AddressingRelative();
            bool ps = (ad & 0xFF00) == (PC & 0xFF00);
            Bne(ad);
            if (PC == ad)
            {
                cycles++;
                if (!ps)
                    cycles++;
            }
            return cycles;
        }

        /// <summary>
        /// CMP (oper), Y
        /// 累加器与内存比较（间接变址型）
        /// </summary>
        [Instruction(true, 2, 5, true)]
        protected int CmdD1()
        {
            addr ad = AddressingIndirectIndexed();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            Cmp(ad);
            return 5 + (ps ? 0 : 1);
        }

        /// <summary>
        /// KIL
        /// </summary>
        [Instruction(false, 1, 1)]
        protected int CmdD2()
        {
            Kil();
            return 0;
        }

        /// <summary>
        /// DCP (oper), Y
        /// （间接变址型）
        /// </summary>
        [Instruction(false, 2, 8)]
        protected int CmdD3()
        {
            addr ad = AddressingIndirectIndexed();
            Dcp(ad);
            return 8;
        }

        /// <summary>
        /// Nop
        /// 零页X变址
        /// </summary>
        [Instruction(false, 2, 4)]
        protected int CmdD4()
        {
            addr ad = AddressingZeroPageX();
            Nop(ad);
            return 4;
        }

        /// <summary>
        /// CMP oper, X
        /// 累加器与内存比较（零页X变址）
        /// </summary>
        [Instruction(true, 2, 4)]
        protected int CmdD5()
        {
            addr ad = AddressingZeroPageX();
            Cmp(ad);
            return 4;
        }

        /// <summary>
        /// DEC oper, X
        /// 存储器减1（零页X变址）
        /// </summary>
        [Instruction(true, 2, 6)]
        protected int CmdD6()
        {
            addr ad = AddressingZeroPageX();
            Dec(ad);
            return 6;
        }

        /// <summary>
        /// DCP oper, X
        /// （零页X变址）
        /// </summary>
        [Instruction(false, 2, 6)]
        protected int CmdD7()
        {
            addr ad = AddressingZeroPageX();
            Dcp(ad);
            return 6;
        }

        /// <summary>
        /// CLD
        /// 清十进制方式
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int CmdD8()
        {
            Cld();
            return 2;
        }

        /// <summary>
        /// CMP oper, Y
        /// 累加器与内存比较（绝对Y变址）
        /// </summary>
        [Instruction(true, 3, 4, true)]
        protected int CmdD9()
        {
            addr ad = AddressingAbsoluteY();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            Cmp(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// NOP
        /// </summary>
        [Instruction(false, 1, 2)]
        protected int CmdDA()
        {
            Nop();
            return 2;
        }

        /// <summary>
        /// DCP oper, Y
        /// （绝对Y变址）
        /// </summary>
        [Instruction(false, 3, 7)]
        protected int CmdDB()
        {
            addr ad = AddressingAbsoluteY();
            Dcp(ad);
            return 7;
        }

        /// <summary>
        /// Nop
        /// 绝对X变址
        /// </summary>
        [Instruction(false, 3, 4, true)]
        protected int CmdDC()
        {
            addr ad = AddressingAbsoluteX();
            bool ps = ((ad - X) & 0xFF00) == (ad & 0xFF00);
            Nop(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// CMP oper, X
        /// 累加器与内存比较（绝对X变址）
        /// </summary>
        [Instruction(true, 3, 4, true)]
        protected int CmdDD()
        {
            addr ad = AddressingAbsoluteX();
            bool ps = ((ad - X) & 0xFF00) == (ad & 0xFF00);
            Cmp(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// DEC oper, X
        /// 存储器减1（绝对X变址）
        /// </summary>
        [Instruction(true, 3, 7)]
        protected int CmdDE()
        {
            addr ad = AddressingAbsoluteX();
            Dec(ad);
            return 7;
        }

        /// <summary>
        /// DCP oper, X
        /// （绝对X变址）
        /// </summary>
        [Instruction(false, 3, 7)]
        protected int CmdDF()
        {
            addr ad = AddressingAbsoluteX();
            Dcp(ad);
            return 7;
        }

        #endregion

        #region ==============================| 6502 CPU 指令 Ex |==============================

        /// <summary>
        /// CPX #oper
        /// X与立即数比较
        /// </summary>
        [Instruction(true, 2, 2)]
        protected int CmdE0()
        {
            byte op = AddressImmediate();
            Cpx(op);
            return 2;
        }

        /// <summary>
        /// SBC (oper, X)
        /// 带借位减法
        /// </summary>
        [Instruction(true, 2, 6)]
        protected int CmdE1()
        {
            addr ad = AddressingIndexedIndirect();
            Sbc(ad);
            return 6;
        }

        /// <summary>
        /// NOP #oper
        /// 空操作
        /// </summary>
        [Instruction(false, 2, 2)]
        protected int CmdE2()
        {
            byte op = AddressImmediate();
            Nop(op);
            return 2;
        }

        /// <summary>
        /// ISC (oper, X)
        /// （索引间接型）
        /// </summary>
        [Instruction(false, 2, 8)]
        protected int CmdE3()
        {
            addr ad = AddressingIndexedIndirect();
            Isc(ad);
            return 8;
        }

        /// <summary>
        /// CPX oper
        /// 变址寄存器X与内存比较（零页）
        /// </summary>
        [Instruction(true, 2, 3)]
        protected int CmdE4()
        {
            addr ad = AddressingZeroPage();
            Cpx(ad);
            return 3;
        }

        /// <summary>
        /// SBC oper
        /// 带借位减法（零页）
        /// </summary>
        [Instruction(true, 2, 3)]
        protected int CmdE5()
        {
            addr ad = AddressingZeroPage();
            Sbc(ad);
            return 3;
        }

        /// <summary>
        /// INC oper
        /// 存储器加1（零页）
        /// </summary>
        [Instruction(true, 2, 5)]
        protected int CmdE6()
        {
            addr ad = AddressingZeroPage();
            Inc(ad);
            return 5;
        }

        /// <summary>
        /// ISC oper
        /// （零页）
        /// </summary>
        [Instruction(false, 2, 5)]
        protected int CmdE7()
        {
            addr ad = AddressingZeroPage();
            Isc(ad);
            return 5;
        }

        /// <summary>
        /// INX
        /// 变址寄存器X加1
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int CmdE8()
        {
            Inx();
            return 2;
        }

        /// <summary>
        /// SBC #oper
        /// 带借位减法（立即型）
        /// </summary>
        [Instruction(true, 2, 2)]
        protected int CmdE9()
        {
            byte op = AddressImmediate();
            Sbc(op);
            return 2;
        }

        /// <summary>
        /// NOP
        /// （隐含型）
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int CmdEA()
        {
            Nop();
            return 2;
        }

        /// <summary>
        /// USBC #oper
        /// SBC后加NOP(立即型)
        /// </summary>
        [Instruction(false, 2, 2)]
        protected int CmdEB()
        {
            byte op = AddressImmediate();
            Sbc(op);
            return 2;
        }

        /// <summary>
        /// CPX oper
        /// 变址寄存器X与内存比较（绝对型）
        /// </summary>
        [Instruction(true, 3, 4)]
        protected int CmdEC()
        {
            addr ad = AddressingAbsolute();
            Cpx(ad);
            return 4;
        }

        /// <summary>
        /// SBC oper
        /// 带借位减法（绝对型）
        /// </summary>
        [Instruction(true, 3, 4)]
        protected int CmdED()
        {
            addr ad = AddressingAbsolute();
            Sbc(ad);
            return 4;
        }

        /// <summary>
        /// INC oper
        /// 存储器加1（绝对型）
        /// </summary>
        [Instruction(true, 3, 6)]
        protected int CmdEE()
        {
            addr ad = AddressingAbsolute();
            Inc(ad);
            return 6;
        }

        /// <summary>
        /// ISC oper
        /// （绝对型）
        /// </summary>
        [Instruction(false, 3, 6)]
        protected int CmdEF()
        {
            addr ad = AddressingAbsolute();
            Isc(ad);
            return 6;
        }

        #endregion

        #region ==============================| 6502 CPU 指令 Fx |==============================

        /// <summary>
        /// BEQ oper
        /// Z=1 分支
        /// </summary>
        [Instruction(true, 2, 2, true, true)]
        protected int CmdF0()
        {
            int cycles = 2;
            addr ad = AddressingRelative();
            bool ps = (ad & 0xFF00) == (PC & 0xFF00);
            Beq(ad);
            if (PC == ad)
            {
                cycles++;
                if (!ps) cycles++;
            }
            return cycles;
        }

        /// <summary>
        /// SBC (oper), Y
        /// 带借位减法（间接变址型）
        /// </summary>
        [Instruction(true, 2, 5, true)]
        protected int CmdF1()
        {
            addr ad = AddressingIndirectIndexed();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            Sbc(ad);
            return 5 + (ps ? 0 : 1);
        }

        /// <summary>
        /// KIL
        /// </summary>
        [Instruction(false, 1, 1)]
        protected int CmdF2()
        {
            Kil();
            return 0;
        }

        /// <summary>
        /// ISC (oper), Y
        /// 间接变址型
        /// </summary>
        [Instruction(false, 2, 8)]
        protected int CmdF3()
        {
            addr ad = AddressingIndirectIndexed();
            Isc(ad);
            return 8;
            //  这个指令：
            //  https://www.masswerk.at/6502/6502_instruction_set.html#ISC 注明是4个CPU周期，
            //  但 nestest.log 中表明运行了8个周期
            //  不知道哪个对
        }

        /// <summary>
        /// Nop
        /// 零页X变址
        /// </summary>
        [Instruction(false, 2, 4)]
        protected int CmdF4()
        {
            addr ad = AddressingZeroPageX();
            Nop(ad);
            return 4;
        }

        /// <summary>
        /// SBC oper, X
        /// 带借位减法（零页X变址）
        /// </summary>
        [Instruction(true, 2, 4)]
        protected int CmdF5()
        {
            addr ad = AddressingZeroPageX();
            Sbc(ad);
            return 4;
        }

        /// <summary>
        /// INC oper, X
        /// 存储器加1（零页X变址）
        /// </summary>
        [Instruction(true, 2, 6)]
        protected int CmdF6()
        {
            addr ad = AddressingZeroPageX();
            Inc(ad);
            return 6;
        }

        /// <summary>
        /// ISC oper, X
        /// （零页X变址）
        /// </summary>
        [Instruction(false, 2, 6)]
        protected int CmdF7()
        {
            addr ad = AddressingZeroPageX();
            Isc(ad);
            return 6;
        }

        /// <summary>
        /// SED
        /// 置十进制标志
        /// </summary>
        [Instruction(true, 1, 2)]
        protected int CmdF8()
        {
            Sed();
            return 2;
        }

        /// <summary>
        /// SBC oper, Y
        /// 带借位减法（绝对Y变址型）
        /// </summary>
        [Instruction(true, 3, 4, true)]
        protected int CmdF9()
        {
            addr ad = AddressingAbsoluteY();
            bool ps = ((ad - Y) & 0xFF00) == (ad & 0xFF00);
            Sbc(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// NOP
        /// </summary>
        [Instruction(false, 1, 2)]
        protected int CmdFA()
        {
            Nop();
            return 2;
        }

        /// <summary>
        /// ISC oper, Y
        /// （绝对Y变址）
        /// </summary>
        [Instruction(false, 3, 7)]
        protected int CmdFB()
        {
            addr ad = AddressingAbsoluteY();
            Isc(ad);
            return 7;
        }

        /// <summary>
        /// Nop
        /// 绝对X变址
        /// </summary>
        [Instruction(false, 3, 4, true)]
        protected int CmdFC()
        {
            addr ad = AddressingAbsoluteX();
            bool ps = ((ad - X) & 0xFF00) == (ad & 0xFF00);
            Nop(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// SBC oper, X
        /// 带借位减法（绝对X变址型）
        /// </summary>
        [Instruction(true, 3, 4, true)]
        protected int CmdFD()
        {
            addr ad = AddressingAbsoluteX();
            bool ps = ((ad - X) & 0xFF00) == (ad & 0xFF00);
            Sbc(ad);
            return 4 + (ps ? 0 : 1);
        }

        /// <summary>
        /// INC oper, X
        /// 存储器加1（绝对X变址）
        /// </summary>
        [Instruction(true, 3, 7)]
        protected int CmdFE()
        {
            addr ad = AddressingAbsoluteX();
            Inc(ad);
            return 7;
        }

        /// <summary>
        /// ISC oper, X
        /// （绝对X变址）
        /// </summary>
        [Instruction(false, 3, 7)]
        protected int CmdFF()
        {
            addr ad = AddressingAbsoluteX();
            Isc(ad);
            return 7;
        }

        #endregion

        //================================================================================
        //  以下为各CMD的操作数的字节数
        //================================================================================
        internal static int[] CmdLen = new int[256]
        {
            //       x3          x7          xB          xF
            1, 2, 1, 2, 2, 2, 2, 2, 1, 2, 1, 2, 3, 3, 3, 3, //  0x
            2, 2, 1, 2, 2, 2, 2, 2, 1, 3, 1, 3, 3, 3, 3, 3, //  1x
            3, 2, 1, 2, 2, 2, 2, 2, 1, 2, 1, 2, 3, 3, 3, 3, //  2x
            2, 2, 1, 2, 2, 2, 2, 2, 1, 3, 1, 3, 3, 3, 3, 3, //  3x
            1, 2, 1, 2, 2, 2, 2, 2, 1, 2, 1, 2, 3, 3, 3, 3, //  4x
            2, 2, 1, 2, 2, 2, 2, 2, 1, 3, 1, 3, 3, 3, 3, 3, //  5x
            1, 2, 1, 2, 2, 2, 2, 2, 1, 2, 1, 2, 3, 3, 3, 3, //  6x
            2, 2, 1, 2, 2, 2, 2, 2, 1, 3, 1, 3, 3, 3, 3, 3, //  7x
            2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 1, 2, 3, 3, 3, 3, //  8x
            2, 2, 1, 2, 2, 2, 2, 2, 1, 3, 1, 3, 3, 3, 3, 3, //  9x
            2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 1, 2, 3, 3, 3, 3, //  Ax
            2, 2, 1, 2, 2, 2, 2, 2, 1, 3, 1, 3, 3, 3, 3, 3, //  Bx
            2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 1, 2, 3, 3, 3, 3, //  Cx
            2, 2, 1, 2, 2, 2, 2, 2, 1, 3, 1, 3, 3, 3, 3, 3, //  Dx
            2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 1, 2, 3, 3, 3, 3, //  Ex
            2, 2, 1, 2, 2, 2, 2, 2, 1, 3, 1, 3, 3, 3, 3, 3  //  Fx
        };
    }
}